package com.echo.ssm.controller;

import java.util.List;
import java.util.Random;

import org.apache.ibatis.ognl.ObjectMethodAccessor;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.echo.ssm.model.Son;
import com.echo.ssm.redis.RedisUtil;
import com.echo.ssm.service.SonService;



/***
 * 
 * @author tangsixiang
 *
 */
@Controller
@RequestMapping("/soncontroller")
public class SonController {

	@Autowired
	private  SonService sonService;
	
	/**
	 * 
	 * @param son
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/saveData",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public  Son saveSonData(Son son,Model model) throws Exception{
		 int count=sonService.insertSelective(son);
		return son;
	}
/*	@RequestMapping(value="/saveData",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public  Son saveSonData(@RequestBody String json) throws Exception{
		return son;
	}*/
	/*@RequestMapping(value="/saveData",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public  String saveSonDataStr(Son son,Model model) throws Exception{
		 int count=sonService.insertSelective(son);
		 return  new  ObjectMapper().writeValueAsString(son);
	}
	*/
	
	/**
	 * 
	 * @param son
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/selectData",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String   saveSonData(Integer pageSize,Integer  pageNum) throws Exception{
		long    startTime=      System.currentTimeMillis();
		    String SonListKey="son:list:page:[%s]:[%s]";
		    String  sonListRedisKey=String.format(SonListKey, pageSize,pageNum);
		     if(RedisUtil.exists(sonListRedisKey)){
		    	 System.out.println("---直接从缓存中获取值----key:"+sonListRedisKey);
		    	 System.out.println(System.currentTimeMillis()-startTime);
		    	 return   RedisUtil.findStr(sonListRedisKey);
		    	 
		     }else{
		    	System.out.println("---没有缓存 生产缓存--");
		    	 List<Son> sonList = sonService.selectByPagesList(pageSize, pageNum);
		    	 String redisSonListStr= new ObjectMapper().writeValueAsString(sonList);
		    	 RedisUtil.addStr(sonListRedisKey, redisSonListStr);
		    	 System.out.println(System.currentTimeMillis()-startTime);
		    	 return redisSonListStr;
		     }
	}
	
	
	
	
	
	
	@RequestMapping(value="/pushData",produces={"text/event-stream"})
	public @ResponseBody  String  saveSonDataPush() throws Exception{
		  Random r =new Random();
		    try {
		    //我更新了sonController代码
				Thread.sleep(1000);
			} catch (Exception e) {
				 
			}
		return "";
	}
}
